package vn.bk.hotel;

import vn.bk.hotel.dialog.DialogChooseLanguage;
import vn.bk.hotel.dialog.DialogExit;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainMenuActivity extends BaseActivity {

    DrawerLayout mDrawerLayout;
    View mDrawerList;
    ActionBarDrawerToggle mDrawerToggle;
    private String[] mFragmentTitles;
    int[] icon;
    boolean firstClick = false;
    private final int REQUEST_CODE = 200;
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main_menu);

	// Locate DrawerLayout in drawer_main.xml
	mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

	// Locate ListView in drawer_main.xml
	mDrawerList = (View) findViewById(R.id.left_drawer);

	initTextViews();

	mFragmentTitles = getResources().getStringArray(R.array.titles_array);

	// Enable ActionBar app icon to behave as action to toggle nav drawer
	// getSupportActionBar().setHomeButtonEnabled(true);
	// getSupportActionBar().setDisplayHomeAsUpEnabled(true);

	// ActionBarDrawerToggle ties together the the proper interactions
	// between the sliding drawer and the action bar app icon
	mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
		R.drawable.ic_drawer, R.string.drawer_open,
		R.string.drawer_close) {

	    public void onDrawerClosed(View view) {
		// TODO Auto-generated method stub
		super.onDrawerClosed(view);
	    }

	    public void onDrawerOpened(View drawerView) {
		// TODO Auto-generated method stub
		super.onDrawerOpened(drawerView);

	    }
	};

	mDrawerLayout.setDrawerListener(mDrawerToggle);

	if (savedInstanceState == null) {
	    selectItem(0);
	}

    }

    private void initTextViews() {
	TextView searchLocation = (TextView) mDrawerList
		.findViewById(R.id.search_location);
	searchLocation.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(0);
	    }
	});

	// TextView searchLandmark = (TextView) mDrawerList
	// .findViewById(R.id.search_landmark);
	// searchLandmark.setOnClickListener(new OnClickListener() {
	//
	// @Override
	// public void onClick(View arg0) {
	// selectItem(1);
	// }
	// });

	LinearLayout searchHN = (LinearLayout) mDrawerList
		.findViewById(R.id.layout_title_hanoi);
	searchHN.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(2);
	    }
	});

	LinearLayout searchHCM = (LinearLayout) mDrawerList
		.findViewById(R.id.layout_title_hcm);
	searchHCM.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(3);
	    }
	});

	LinearLayout searchDN = (LinearLayout) mDrawerList
		.findViewById(R.id.layout_title_danang);
	searchDN.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(4);
	    }
	});

	LinearLayout searchHP = (LinearLayout) mDrawerList
		.findViewById(R.id.layout_title_haiphong);
	searchHP.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(5);
	    }
	});
	LinearLayout searchKH = (LinearLayout) mDrawerList
		.findViewById(R.id.layout_title_khanhhoa);
	searchKH.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(6);
	    }
	});
	LinearLayout searchBRVT = (LinearLayout) mDrawerList
		.findViewById(R.id.layout_title_baria_vt);
	searchBRVT.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(7);
	    }
	});
	LinearLayout searchLD = (LinearLayout) mDrawerList
		.findViewById(R.id.layout_title_lamdong);
	searchLD.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(8);
	    }
	});

	TextView favoriteHotel = (TextView) mDrawerList
		.findViewById(R.id.favorite_hotel);
	favoriteHotel.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(9);
	    }
	});

	TextView setLanguage = (TextView) mDrawerList
		.findViewById(R.id.set_language);
	setLanguage.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(10);
	    }
	});

	TextView aboutApp = (TextView) mDrawerList
		.findViewById(R.id.about_apps);
	aboutApp.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		selectItem(11);
	    }
	});

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	getSupportMenuInflater().inflate(R.menu.main, menu);
	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

	// if (item.getItemId() == android.R.id.h) {
	firstClick = false;
	if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
	    mDrawerLayout.closeDrawer(mDrawerList);

	} else {
	    mDrawerLayout.openDrawer(mDrawerList);
	}
	// }

	return false;
    }

    // The click listener for ListView in the navigation drawer

    @Override
    public void setTitle(CharSequence title) {
	// TODO Auto-generated method stub
	getSupportActionBar().setTitle(title);
    }
    
    

    @Override
    protected void onResume() {
	// TODO Auto-generated method stub
	super.onResume();
	if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
	    mDrawerLayout.closeDrawer(mDrawerList);

	} else {
	    mDrawerLayout.openDrawer(mDrawerList);
	}
    }

    private void selectItem(int position) {
	firstClick = false;
	FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
	// Locate Position
	Log.i("Selection Item", String.valueOf(position));
	SherlockFragment fragment = null;
	;
	switch (position) {
	case 0:
	    fragment = new FavoriteHotel();
	    break;
	// case 1:
	// fragment = new SearchByLandmark();
	// break;
	case 2:
	    ChooseDistrict.nameCity = 0;
	    fragment = new ChooseDistrict();
	    break;
	case 3:
	    ChooseDistrict.nameCity = 1;
	    fragment = new ChooseDistrict();
	    break;
	case 4:
	    ChooseDistrict.nameCity = 2;
	    fragment = new ChooseDistrict();
	    break;
	case 5:
	    ChooseDistrict.nameCity = 3;
	    fragment = new ChooseDistrict();
	    break;
	case 6:
	    ChooseDistrict.nameCity = 4;
	    fragment = new ChooseDistrict();
	    break;
	case 7:
	    ChooseDistrict.nameCity = 5;
	    fragment = new ChooseDistrict();
	    break;
	case 8:
	    ChooseDistrict.nameCity = 6;
	    fragment = new ChooseDistrict();
	    break;
	case 9:
	    fragment = new FavoriteHotel();
	    break;
	case 10:
	    break;
	case 11:
	    fragment = new About();
	    break;
	default:
	    fragment = new FavoriteHotel();
	    break;
	}
	if (position == 10) {

	    mDrawerLayout.closeDrawer(mDrawerList);
	    DialogChooseLanguage dialog = new DialogChooseLanguage(
		    MainMenuActivity.this);
	    dialog.show();
	} else if (position == 0) {
	    Intent searchIntent = new Intent(MainMenuActivity.this, SearchActivity.class);
	    startActivityForResult(searchIntent, REQUEST_CODE);
	    
	} else {
	    ft.replace(R.id.content_frame, fragment);
	    ft.commit();
	    // mDrawerList.setItemChecked(position, true);
	    // Close drawer
	    setTitle(mFragmentTitles[position]);
	    mDrawerLayout.closeDrawer(mDrawerList);
	}
    }
    

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
	super.onPostCreate(savedInstanceState);
	// Sync the toggle state after onRestoreInstanceState has occurred.
	mDrawerToggle.syncState();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent arg2) {
	// TODO Auto-generated method stub
	super.onActivityResult(requestCode, resultCode, arg2);
	Log.i("REQUEST CODE", String.valueOf(requestCode));
	Log.i("RESULT CODE", String.valueOf(resultCode));

	if (requestCode == REQUEST_CODE) {
	    if (resultCode == 0) {
		Log.i("tag", "Start detect");
		selectItem(9);
	    }
	}
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
	super.onConfigurationChanged(newConfig);
	// Pass any configuration change to the drawer toggles
	mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onPause() {
	// TODO Auto-generated method stub
	super.onPause();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	// TODO Auto-generated method stub
	if (keyCode == KeyEvent.KEYCODE_BACK) {
//	    DialogExit dialog = new DialogExit(MainMenuActivity.this);
//	    dialog.show();
	    if(firstClick){
		finish();
	    }else{
		firstClick = true;
		Toast.makeText(getApplicationContext(), getString(R.string.press_back_again_exit), Toast.LENGTH_SHORT).show();
	    }
	} else if (keyCode == KeyEvent.KEYCODE_MENU) {
	    firstClick = false;
	    if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
		mDrawerLayout.closeDrawer(mDrawerList);
	    } else {
		mDrawerLayout.openDrawer(mDrawerList);
	    }
	}
	
	return false;
    }

}
